package com.gz.DroidPong;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.SmoothCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;

import com.gz.DroidPong.Constants.SceneManager;
import com.gz.DroidPong.Constants.UIManager;
import com.gz.DroidPong.Scenes.LoadingScene;

public class DroidPongActivity extends BaseGameActivity {
    /** Called when the activity is first created. */
	private static int CAMERA_WIDTH = 720;
	private static int CAMERA_HEIGHT = 480;
	private static float LEVEL_WIDTH = 720;
	private static float LEVEL_HEIGHT = 480;
	
	private Engine mEngine;
	private SmoothCamera mSmoothCamera;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        log("Init");
        
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
    	if (keyCode == KeyEvent.KEYCODE_BACK) {
    		//Ask the user if they want to quit

    		if(SceneManager.getCurrentSceneID() == SceneManager.SCENE_MENU){
    			
    			new AlertDialog.Builder(this)
        		.setIcon(android.R.drawable.ic_dialog_alert)
        		.setTitle("Exit")
        		.setMessage("Are you sure you want to leave?")
        		.setNegativeButton(android.R.string.cancel, null)
        		.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        		
        			public void onClick(DialogInterface dialog, int which){
    	    		// Exit the activity
        				DroidPongActivity.this.finish();
    	    		}
    	    	})
    	    	.show();
    	    	// Say that we've consumed the event
    	    	
        		return true;
    		}
    		else{
    			mEngine.setScene(SceneManager.getMenuScene());
    			SceneManager.setCurrentScene(SceneManager.SCENE_MENU);
    			return true;
    		}
    		
    		
    	}
    		 
    	return super.onKeyDown(keyCode, event);
    }
    
	public Engine onLoadEngine() {
		log("Loading Engine");
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		CAMERA_WIDTH = dm.widthPixels;
		CAMERA_HEIGHT = dm.heightPixels;
		
		LEVEL_WIDTH = 1.25f*CAMERA_WIDTH;
		LEVEL_HEIGHT = 1.25f*CAMERA_HEIGHT;
		
		float maxPanVelocityX = 100;
		float maxPanVelocityY = 40;
		float maxZoomVel = 1;
		
		log("Setting activity. w:" + CAMERA_WIDTH + " h:" + CAMERA_HEIGHT);
		UIManager.setActivity(this, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		log("Initialize camera");
		this.mSmoothCamera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, maxPanVelocityX,maxPanVelocityY,maxZoomVel);
		
		log("Initialize engine");
		mEngine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mSmoothCamera)); 
		
		log("SetScene new Loading scene");
		SceneManager.setScene(SceneManager.SCENE_LOADING,new LoadingScene(3,mEngine,LoadingScene.TARGET_MENU_SCENE));
		
		return mEngine;
	}

	public void onLoadResources() {
		log("LoadResources");
		SceneManager.getLoadingScene().onLoadResources();
	}

	public Scene onLoadScene() {
		log("Loading Scene - new LoadingScene");
		
		SceneManager.getLoadingScene().onLoadScene();
		SceneManager.setCurrentScene(SceneManager.SCENE_LOADING);
		return SceneManager.getLoadingScene();
	}

	public void onLoadComplete() {
		
		log("Load Compete");
	}
	
	public int GetIntPreference(String pVariableName, Integer pDefault) {
		final SharedPreferences prefs = 
			this.getPreferences(Context.MODE_PRIVATE);
		Integer intReturn =prefs.getInt(pVariableName, 99); 
		log("Getting int " + pVariableName + " = " + intReturn.toString());		
		return intReturn;
	}

	public void SetIntPreference(String pVariableName,Integer pValue) {
		log("Saving int " + pVariableName + " = " + pValue.toString());
		
		final SharedPreferences prefs = 
			this.getPreferences(Context.MODE_PRIVATE);
		prefs.edit().putInt(pVariableName, pValue).commit();
		
		log("Get after save " + prefs.getInt(pVariableName, 99));
	}
	
	public Boolean GetBoolPreference(String pVariableName, Boolean pDefault) {
		
		final SharedPreferences prefs = 
			this.getPreferences(Context.MODE_PRIVATE);
		Boolean blnReturn = prefs.getBoolean(pVariableName, true);
		log("Getting boolean " + pVariableName + " = " + blnReturn.toString());
		return blnReturn;
	}

	public void SetBoolPreference(String pVariableName,Boolean pValue) {
		log("Saving boolean " + pVariableName + " = " + pValue.toString());
		final SharedPreferences prefs = 
			this.getPreferences(Context.MODE_PRIVATE);
		prefs.edit().putBoolean(pVariableName, pValue).commit();
	}
	
	private void log(String msg){
		Log.d("Game Activity",msg);
	}
}